package top.hidoctor.auscultation.net.p;

import android.content.Context;
import android.text.TextUtils;

import com.tang.base.BaseResponse;
import com.tang.base.NetMapEncryptUtils;
import com.tang.base.PatchRequest;
import com.tang.net.SuccessConsumer;
import com.tang.util.GsonUtil;

import java.io.File;
import java.util.Map;

import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import top.hidoctor.auscultation.bean.AuditBean;
import top.hidoctor.auscultation.bean.DictionaryBean;
import top.hidoctor.auscultation.bean.Hospitalbean;
import top.hidoctor.auscultation.bean.User;
import top.hidoctor.auscultation.net.PresenterWrapper;
import top.hidoctor.auscultation.net.c.AuditDetailContract;
import top.hidoctor.auscultation.net.c.SettingContract;
import top.hidoctor.auscultation.net.req.DictionaryReq;
import top.hidoctor.auscultation.net.req.UsersReq;

public class SettingPresenter extends PresenterWrapper<SettingContract.View> implements SettingContract.Presenter {


    public SettingPresenter(Context context, SettingContract.View view) {
        super(context, view);
    }

    @Override
    public void getDictionary(final DictionaryReq req) {
        mView.showLoading();
        Map<String, Object> maps = new NetMapEncryptUtils(req).getMaps();
        add(mService.dictionaryList(maps)
                .compose(this.getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<Object>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<Object> response) {
                        mView.hideLoading();
                        String data = GsonUtil.toJsonStr(response.getData());
                        if (!TextUtils.isEmpty(data)) {
                            DictionaryBean bean = GsonUtil.parseJson(data, DictionaryBean.class);
                            mView.getDictionarySuc(bean);
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }

    @Override
    public void updateInfo(String id, UsersReq req) {
        mView.showLoading();
        Map<String, Object> maps = new NetMapEncryptUtils(req).getMaps();
        add(mService.updateUserInfo(id, new PatchRequest(maps).getRequestBody())
                .compose(this.getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<Object>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<Object> response) {
                        mView.hideLoading();
                        mView.updateInfoSuc();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }

    @Override
    public void getHospitalList() {
        mView.showLoading();
        final Map<String, Object> maps = new NetMapEncryptUtils("").getMaps();
        add(mService.getHospital(maps)
                .compose(this.<Hospitalbean>getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<Hospitalbean>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<Hospitalbean> response) {
                        mView.hideLoading();
                        mView.getHospitalListSuc(response.getData());

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }

    @Override
    public void getUser(String id) {
        mView.showLoading();
        Map<String, Object> map = new NetMapEncryptUtils("").getMaps();
        add(mService.getUserInfo(id, map)
                .compose(this.<User>getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<User>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<User> response) {
                        mView.hideLoading();
                        mView.getUserSuc(response.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }

    public void uploadFile(String uri, final int types) {
        File file = new File(uri);
        RequestBody requestFile = RequestBody.create(MediaType.parse("image/jpeg;charset=utf-8"), file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("fileList", file.getName(), requestFile);
        final RequestBody type = RequestBody.create(MediaType.parse("multipart/form-data"), "0");
        mView.showLoading();
        add(mService.uploadFile(type, body)
                .compose(this.<String>getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<String>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<String> response) {
                        mView.hideLoading();
                        mView.getFileScu(response.getData(), types);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }

    public void completeInfo(String userId, UsersReq usersReq) {
        mView.showLoading();
        Map<String, Object> maps = new NetMapEncryptUtils(usersReq).getMaps();
        add(mService.completeInfo(userId, new PatchRequest(maps).getRequestBody())
                .compose(this.getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<Object>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<Object> response) {
                        mView.hideLoading();
                        mView.updateInfoSuc();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }
}
